package com.c2soft.nepalcmx.controller;

import java.util.List;

import javax.xml.ws.RequestWrapper;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.c2soft.nepalcmx.command.AccountDetailCommand;
import com.c2soft.nepalcmx.command.NetPositionCommand;
import com.c2soft.nepalcmx.command.OrderCommand;
import com.c2soft.nepalcmx.command.TradeBookCommand;
import com.c2soft.nepalcmx.services.OrderMgmtService;

@Controller
@RequestMapping("/order")
public class OrderMgmtController {
	Logger logger = LogManager.getLogger(OrderMgmtController.class.getName());
	
	@Autowired
	private OrderMgmtService orderService;
	
	@RequestMapping(value = "/show")
	public String show() {
		return "testOrder";
	}

	
	@RequestMapping(value = "/buy",method={RequestMethod.GET,RequestMethod.POST}, headers = "Accept=application/json")
	@ResponseBody
	public String buy(@RequestParam("otype") String otype,@RequestParam("insname") String insname,@RequestParam("symbol") String symbol,
			@RequestParam("expdate") String expdate,@RequestParam("qt") String qt,@RequestParam("ql") String ql,
			@RequestParam("qw") String qw,@RequestParam("price") String price,@RequestParam("ptype") String ptype,
			@RequestParam("status") String status,@RequestParam("tp") String tp,@RequestParam("userId") String userId,
			@RequestParam("accno") String accno,@RequestParam("cashl") String cashl,@RequestParam("marginl") String marginl,
			@RequestParam("margin_b") String margin_b) {
		OrderCommand buyOrder=new OrderCommand();
		buyOrder.setOtype(otype);
		buyOrder.setInsname(insname);
		buyOrder.setSymbol(symbol);
		buyOrder.setExpdate(expdate);
		buyOrder.setQt(qt);
		buyOrder.setQl(ql);
		buyOrder.setQw(qw);
		buyOrder.setPrice(price);
		buyOrder.setPtype(ptype);
		buyOrder.setStatus(status);
		buyOrder.setTp(tp);
		buyOrder.setUserId(userId);
		buyOrder.setAccno(accno);
		buyOrder.setCashl(cashl);
		buyOrder.setMarginl(marginl);
		buyOrder.setMargin_b(margin_b);
		logger.debug(buyOrder);
		logger.debug(buyOrder.getUserId());
		orderService.buyService(buyOrder);
		return "{\"msg\":\"Buy Details added successfully.\"}";
	}
	

	@RequestMapping(value = "/sell", method = { RequestMethod.GET,RequestMethod.POST },  headers = "Accept=application/json")
	
	public @ResponseBody String sell(@RequestParam("otype") String otype,@RequestParam("insname") String insname,@RequestParam("symbol") String symbol,
			@RequestParam("expdate") String expdate,@RequestParam("qt") String qt,@RequestParam("ql") String ql,
			@RequestParam("qw") String qw,@RequestParam("price") String price,@RequestParam("ptype") String ptype,
			@RequestParam("status") String status,@RequestParam("tp") String tp,@RequestParam("userId") String userId,
			@RequestParam("accno") String accno,@RequestParam("cashl") String cashl,@RequestParam("marginl") String marginl,
			@RequestParam("margin_b") String margin_b) {
		OrderCommand sellOrder=new OrderCommand();
		sellOrder.setOtype(otype);
		sellOrder.setInsname(insname);
		sellOrder.setSymbol(symbol);
		sellOrder.setExpdate(expdate);
		sellOrder.setQt(qt);
		sellOrder.setQl(ql);
		sellOrder.setQw(qw);
		sellOrder.setPrice(price);
		sellOrder.setPtype(ptype);
		sellOrder.setStatus(status);
		sellOrder.setTp(tp);
		sellOrder.setUserId(userId);
		sellOrder.setAccno(accno);
		sellOrder.setCashl(cashl);
		sellOrder.setMarginl(marginl);
		sellOrder.setMargin_b(margin_b);
		logger.debug(sellOrder);
		logger.debug(sellOrder.getUserId());
		orderService.sellService(sellOrder);
		return "{\"msg\":\"Sell Details added successfully.\"}";
	}
	             	
@RequestMapping(value = "/buyorderMgmt",method=RequestMethod.GET, headers = "Accept=application/json")
	
	public @ResponseBody List<NetPositionCommand> getOrders(@RequestParam("userId") String userId) {
		List<NetPositionCommand> accountDetail=orderService.getBuyOrder(userId);
		return accountDetail;
	}



}
